#include "timer.h"
#include "led.h"
#include "speed.h"

void TIM_Init(uint16_t time)
{
	//定义结构体
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//定时器时钟使能
	
	//初始化定时器，配置ARR，PSC
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
	TIM_TimeBaseInitStruct.TIM_Period = time;//自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 4800-1;//定时器分频
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);//初始化TIM3

	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能定时器中断
//	TIM3->DIER |= ENABLE;
	
	//开启定时器中断，配置NVIC
	NVIC_InitStruct.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;//优先级3
	NVIC_Init(&NVIC_InitStruct);

	TIM_Cmd(TIM3,ENABLE);//使能定时器
}


/* 编写中断服务程序 */
void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
	{
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update);  //清除中断标志位
		getWindSpeed();
		LED1_TIG;
	}
}



